import {ref} from "vue";
import {defineStore} from "pinia";
import {cancelOrderAPI, createNativeAPI, fetchOrderInfoAPI, fetchUserInfoAPI, queryPayStatusAPI, findByDictCodeAPI, userCertationAPI, fetchOrderListAPI, fetchPerssonListAPI, fetchPersonListAPI, fetchOrderStatusListAPI} from "@/api/memberApi.ts";

const useMemberStore = defineStore("memberStore", () => {
    const orderInfo = ref({})
    const userInfo = ref({})
    const certificatesType = ref({})
    const orderListInfo = ref({})
    const personList = ref([])
    const orderStatusList = ref([])
    const fetchOrderInfo = async (orderId: string) => {
        const {data} = await fetchOrderInfoAPI(orderId)
        orderInfo.value = data
    }
    const cancelOrder = async (orderId: string) => {
        const {data} = await cancelOrderAPI(orderId)
        return data
    }

    const createNative = async (orderId: string) => {
        const {data} = await createNativeAPI(orderId)
        return data
    }
    const queryPayStatus = async (orderId: string) => {
        const {data} = await queryPayStatusAPI(orderId)
        return data
    }

    const fetchUserInfo = async () => {
        const {data} = await fetchUserInfoAPI()
        userInfo.value = data
    }

    const findByDictCode = async (dictCode: string) => {
        const {data} = await findByDictCodeAPI(dictCode)
        certificatesType.value = data
    }
    const userCertation = async (formData) => {
        const {message} = await userCertationAPI(formData)
        return message
    }
    const fetchOrderList = async (params) => {
        const {data} = await fetchOrderListAPI(params)
        orderListInfo.value = data
    }

    const fetchPersonList = async () => {
        const {data} = await fetchPersonListAPI()
        const all = {name: "全部就诊人", id: undefined}
        personList.value = [all, ...data]

    }
    const fetchOrderStatus = async () => {
        const {data} = await fetchOrderStatusListAPI()
        const all = {comment: "全部订单状态", status: undefined}
        orderStatusList.value = [all, ...data]

    }
    return {
        orderInfo,
        userInfo,
        certificatesType,
        orderListInfo,
        personList,
        orderStatusList,
        fetchOrderInfo,
        cancelOrder,
        createNative,
        queryPayStatus,
        fetchUserInfo,
        findByDictCode,
        userCertation,
        fetchOrderList,
        fetchPersonList,
        fetchOrderStatus
    }
})

export default useMemberStore
